package com.may.wind.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户信息
 */
@Entity
@Table(name = "users")
@ApiModel(description = "用户信息")
public class User implements UserDetails {
    @Id
    @SequenceGenerator(sequenceName = "user_sequence", name = "sequence_users", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_users")
    @Column(name = "id")
    @ApiModelProperty(name = "id", value = "ID", required = true)
    long id;
    @Column(name = "username")
    @ApiModelProperty(name = "username", value = "用户名", required = true)
    String username;
    @Column(name = "password")
    @ApiModelProperty(name = "password", value = "密码", required = true)
    String password;
    @ApiModelProperty(name = "name", value = "用户名称")
    String name;
    @ApiModelProperty(name = "phone", value = "联系方式")
    String phone;
    @ApiModelProperty(name = "email", value = "邮箱")
    String email;
    @ApiModelProperty(name = "accountNonExpired", value = "账户是否过期")
    boolean accountNonExpired;
    @ApiModelProperty(name = "accountNonLocked", value = "账户是否锁定")
    boolean accountNonLocked;
    @ApiModelProperty(name = "credentialsNonExpired", value = "凭证是否过期")
    boolean credentialsNonExpired;
    @ApiModelProperty(name = "enabled", value = "用户是否可用")
    boolean enabled;
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinTable(name = "users_roles", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "roles_id", referencedColumnName = "id")})
    @ApiModelProperty(name = "roles", value = "用户角色信息")
    List<Role> roles;
    @ApiModelProperty(name = "sessionId", value = "Session ID")
    String sessionId;

    public long getId() {
        return id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role role : getRoles()) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    public List<Role> getRoles() {
        return roles;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
